<?php
require_once 'db/db.php';
require_once 'util/perm.php';

function deleteForum( $cityNum, $forumNum ) {
  if( !hasPerm('delete.forum') ) 
    return -1;

  try {
    $db = openDbConn();
    $db->beginTransaction();
    $db->exec("DELETE FROM forum WHERE forum.id = $forumNum");
    $db->exec("DELETE FROM post WHERE post.tid = (SELECT thread.id FROM thread WHERE thread.fid = $forumNum)");
    $db->exec("DELETE FROM thread WHERE thread.fid = $forumNum");
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}
// int -> int -> string -> string -> bool
// city num as of current is ignored
function setForum( $cityNum, $forumNum, $title, $datum) {
  if( !hasPerm('edit.thread') || !hasPerm('new.thread') ) 
    return -1;

  try {
    $db = openDbConn();

    $cityNum = $db->quote($cityNum);
    $fid = $db->quote($forumNum);
    $title = $db->quote($title);
    $datum = $db->quote($datum);

    $db->beginTransaction();
    if ($forumNum < 1 ) { 

      $db->exec("INSERT INTO forum (rank, title, datum)
                                      VALUES( 1 + (SELECT MAX(id) FROM forum),
                                              $title,
                                              $datum);");
    } else {
      $db->exec("UPDATE forum
                                     SET datum = $datum, title = $title
                                     WHERE id = $fid;");
    }
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}
?>
